package com.gsnd.gsnd22estore.service.impl;

import com.gsnd.gsnd22estore.domain.EsUser;
import com.gsnd.gsnd22estore.dto.*;
import com.gsnd.gsnd22estore.exception.BookStoreException;
import com.gsnd.gsnd22estore.mapper.EsUserMapper;
import com.gsnd.gsnd22estore.service.UserService;
import com.gsnd.gsnd22estore.utils.BeanCopyUtils;
import com.gsnd.gsnd22estore.utils.JsonWebTokenUtils;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    EsUserMapper userMapper;

    @Override
    public LoginReturnDto userLogin(UserLoginDto userLd){
        //对userLd进行处理判断（主要是一些）

        if(userLd.getUsername()==null || userLd.getUsername().isEmpty())
        {
            throw new BookStoreException(BookStoreException.CodeMsgEnum.USER_USERNAME_IS_NULL);
        }
        if(StringUtils.isEmpty(userLd.getPassword()))
        {
            throw new BookStoreException(BookStoreException.CodeMsgEnum.USER_PASSWORD_IS_NULL);
        }
        EsUser esUser=userMapper.selectByUsername(userLd.getUsername());
        //根据esuser是否存在进行判断
        if(esUser == null)
        {
            //用户不存在
            throw new BookStoreException(BookStoreException.CodeMsgEnum.USER_IS_NOT_EXIST);
        }
        if(!userLd.getPassword().equals(esUser.getPassword())){
            throw new BookStoreException(BookStoreException.CodeMsgEnum.USER_USERNAME_OR_PASSWORD_ERROR);
        }
        //以上判断都通过，则可以登录，对erUser进行加密
       //正常情况下加密id
        String jwt = JsonWebTokenUtils.createJWT(String.valueOf(esUser.getId()),
                esUser.getUsername(),
                24 * 60 * 60 * 1000L);
        LoginReturnDto loginReturnDto = new LoginReturnDto();
        loginReturnDto.setJwt(jwt);

        return loginReturnDto;
    }

    @Override
    public UserDto getUserInfo(HttpServletRequest request) {
        //通过usrId进行查询用户信息
        //从请求中取回id
        String userId = (String) request.getAttribute("userId");
        //调用Dao层里的sql语句

        return userMapper.selectById(Integer.parseInt(userId));
    }

    @Override
    public void userRegister(UserRegisterDto userRegisterDto) {

        //注册之前检查用户名是否被占用，如有不能注册***（加分项）



        //使用工具将Dto中
        EsUser esUser = BeanCopyUtils.copyBean(userRegisterDto,EsUser.class);
        //将一些没有默认值的内容进行添加
        //注册时间
        esUser.setRegisterTime(LocalDate.from(LocalDateTime.now()));


        esUser.setStatus(0);
        esUser.setRoleId(2);
        esUser.setIsVip(false);
        userMapper.insert(esUser);

    }

    @Override
    public void updatePassword(UpdatePasswordDto updatePasswordDto, HttpServletRequest request) {
        //从请求中取出userId，并且做类型转换
         Integer userId = Integer.parseInt((String) request.getAttribute("userId"));
         //根据UserId查出数据库里的密码对比旧密码
        //UserDto userDto = userMapper.selectById(userId);


        //更新密码
        userMapper.updatePassword(updatePasswordDto.getNewPassword(),userId);
    }

    @Override
    public void updateUser(UpdateUserDto updateUserDto) {

        userMapper.selectById(updateUserDto.getId());
        if(updateUserDto == null)
        {
            throw new BookStoreException(BookStoreException.CodeMsgEnum.USER_IS_NOT_EXIST);
        }
        userMapper.update(updateUserDto);
    }
}
